package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-02
 */
@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService{
     @Autowired
    private MemberReservationMapper memberReservationMapper;

   /**
     * 查询取消预约数量
     *
     * @param userId 用户ID
     * @return 取消预约数量
     */
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        // 调用Mapper方法
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Integer cancelledReservationCount = memberReservationMapper.getCancelledReservationCount(userId, startTime, endTime);
        return cancelledReservationCount==null?0:cancelledReservationCount;
    }

    @Override
    public List<ReservationVo> getReservationList(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<ReservationVo> reservationVo = memberReservationMapper.getReservationList(userId, startTime, endTime, time);
        return reservationVo;
    }
   /**
     * 新增预约
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insetAppointment(Reservation reservation) {
        reservation.setStatus(0);
        boolean save = this.save(reservation);
        return save?1:0;
    }
}
